<?php
  $objXajax->registerFunction("frmroomtype_save");
  $objXajax->registerFunction("frmroomtype_edit");
  $objXajax->registerFunction("frmroomtype_delete");
  $objXajax->registerFunction("frmroomtype_loadGrid");
  $hotel_id = $_SESSION['current']['hotel_id'] ;
  function frmroomtype_save($params){
    //  echo '<pre>'; print_r($params);die();
    global $objDbUpdate,$hotel_id;
    $objResponse = new xajaxResponse();
    $name = addslashes($params['txtnametype']);
    $description = addslashes($params['description']);
    $username = $_SESSION['current']['username'];
    $color = $params['cbbColor'];
    $priceInday = $params['inDay'];    
    $priceOne = $params['oneHour']==''?$priceInday:$params['oneHour'];
    $priceTwo = $params['twoHour']==''?$priceInday:$params['twoHour'];
    $priceThree = $params['threeHour']==''?$priceInday:$params['threeHour'];
    $priceFour= $params['fourHour']==''?$priceInday:$params['fourHour'];
    $priceFive = $params['fiveHour']==''?$priceInday:$params['fiveHour'];
    $flg_quat = $params['chkquat']; 
    $flg_maylanh = $params['chkmaylanh']; 
    $flg_tulanh = $params['chktulanh']; 
    $flg_mnn = $params['chkmnn']; 
    $flg_bontam= $params['chkbontam']; 
    $id = $params['hdId'];
    $edit = $params['hdEdit'];
    $flag = true;

    
    if($params['hdEdit']==1){
        $sqlUpdate = "update tbl_room_type set name ='$name',color='$color',description='$description',
        date_update=now(),user_update='$username',priceone ='$priceOne',pricetwo ='$priceTwo' ,
        pricethree ='$priceThree',pricefour ='$priceFour',pricefive ='$priceFive',priceinday ='$priceInday'
        ,flg_quat ='$flg_quat',flg_maylanh ='$flg_maylanh',flg_mnn ='$flg_mnn',
        flg_bontam ='$flg_bontam',flg_tulanh ='$flg_tulanh' WHERE id='$id' ";
        $flag = $objDbUpdate->Execute($sqlUpdate);
 
    }else{
        $sqlInsert = "insert into tbl_room_type(hotel_id,name,color,description,date_created,date_update,user_created,user_update,
        priceone,pricetwo,pricethree,pricefour,pricefive,priceinday,flg_quat,flg_maylanh,flg_mnn,flg_bontam,flg_tulanh)
         value('$hotel_id','$name','$color','$description',now(),now(),'$username','$username','$priceOne',
         '$priceTwo','$priceThree','$priceFour','$priceFive','$priceInday','$flg_quat','$flg_maylanh','$flg_mnn','$flg_bontam','$flg_tulanh')";
         $flag = $objDbUpdate->Execute($sqlInsert);
         $id_insert = mysql_insert_id();
        $objResponse->addScript("changeUpdate('frmroomtype',$id_insert);");  
    }
     $objResponse->addScript('xajax_frmroomtype_loadGrid();'); 
     $objResponse->addScript('xajax_htfrmmanagerhotel_loadGrid();'); 
     $objResponse->addScript('xajax_htfrmmanagerhotel_Init();');  
      return $objResponse->getXML(); 
  }
  
   function frmroomtype_loadGrid(){
    global $lang,$path;
    $objResponse = new xajaxResponse();
    $lang ='vi';
    $arrGrid = frmroomtype_GetGrid();
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/main_roomtype_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmroomtype_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('frmroomtype_tbl');");
    $objResponse->addScript('changeWaiting(1);');                               
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
  }
  
    function  frmroomtype_GetGrid(){
        global $objDbSelect;
        $hotel_id = $_SESSION['current']['hotel_id'];
        $sqlSelect = "select * from tbl_room_type where hotel_id = '$hotel_id' order by name";
       /// echo $sqlSelect; die();
        return $objDbSelect->GetArray($sqlSelect);
    }
    function frmroomtype_delete($strId){
        global $objDbSelect;
       $objResponse = new xajaxResponse();
       $sql = " delete from tbl_room where room_type in ($strId) " ;
       //echo $sql; die();
       if($objDbSelect->Execute($sql)){
            $sql = " delete from tbl_room_type where id in ($strId) " ; 
            $objDbSelect->Execute($sql);
       }
       $objResponse->addScript('changeWaiting(1);');
       $objResponse->addScript('deleteRowlogic();');
       return $objResponse->getXML(); // dong luon luon co khi goi ham ajax  
    }
    
     function frmroomtype_edit($id){
       global $objDbSelect;
       $objResponse = new xajaxResponse();
       $sql = " SELECT *,REPLACE(REPLACE(description, '\n\r', ' '),'\n',' ') as description1 FROM tbl_room_type WHERE id = '$id' ";
      // echo $sql; die();
       $arr = $objDbSelect->GetArray($sql);
       
      $color        = $arr[0]['color'];
      $priceInday   = $arr[0]['priceinday'];    
      $priceOne     = $arr[0]['priceone'];
      $priceTwo     = $arr[0]['pricetwo'];
      $priceThree   = $arr[0]['pricethree'];
      $priceFour    = $arr[0]['pricefour'];
      $priceFive    = $arr[0]['pricefive'];
      $description  = $arr[0]['description1'];
      $name         = $arr[0]['name'];
      
      $flg_quat         = $arr[0]['flg_quat']==1?'true':'false';
      $flg_maylanh         = $arr[0]['flg_maylanh']==1?'true':'false';
      $flg_tulanh         = $arr[0]['flg_tulanh']==1?'true':'false';
      $flg_mnn         = $arr[0]['flg_mnn']==1?'true':'false';
      $flg_bontam         = $arr[0]['flg_bontam']==1?'true':'false';
      
      $objResponse->addScript("$('#frmroomtype_chkquat').attr('checked',$flg_quat);");
      $objResponse->addScript("$('#frmroomtype_chkmaylanh').attr('checked',$flg_maylanh);");
      $objResponse->addScript("$('#frmroomtype_chktulanh').attr('checked',$flg_tulanh);");
      $objResponse->addScript("$('#frmroomtype_chkmnn').attr('checked',$flg_mnn);");
      $objResponse->addScript("$('#frmroomtype_chkbontam').attr('checked',$flg_bontam);");
      
      $objResponse->addScript("$('#frmroomtype_txtnametype').val('$name');");
      $objResponse->addScript("$('#frmroomtype_cbbColor').val($color);");
      $objResponse->addScript("$('#frmroomtype_cbbColor').removeAttr('class');");
      $objResponse->addScript("$('#frmroomtype_cbbColor').addClass('color$color');");
      $objResponse->addScript("$('#frmroomtype_oneHour').val($priceOne);");
      $objResponse->addScript("$('#frmroomtype_twoHour').val($priceTwo);");
      $objResponse->addScript("$('#frmroomtype_threeHour').val($priceThree);");
      $objResponse->addScript("$('#frmroomtype_fourHour').val($priceFour);");
      $objResponse->addScript("$('#frmroomtype_fiveHour').val($priceFive);");
      $objResponse->addScript("$('#frmroomtype_inDay').val($priceInday);");
      $objResponse->addScript("$('#frmroomtype_description').val('$description');");
      $objResponse->addScript("changeCssEdit('sss');");
      $objResponse->addScript('changeWaiting(1);');
      return $objResponse->getXML();   
    }
?>
